package com.haohope.framework.accesslog;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import com.haohope.framework.cache.ExpireCache;
import com.haohope.framework.cache.GoogleExpireCache;
import com.wb.common.XwlBuffer;

public abstract class AbstractAccesslog{
	protected ExpireCache expireCache = new GoogleExpireCache(5 * 60);
	private JSONObject getXwl(String url) {
		JSONObject info = null;
		try {
			info = XwlBuffer.get(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}

	public void log(HttpServletRequest request,String xwl) {
		HttpSession session = request.getSession(false);
		if (session == null || session.getAttribute("sys.logined") == null) {
			return ;
		}
		String sessionid = session.getId();
		if(expireCache.containsKey(sessionid+"@"+xwl))return;
		JSONObject json = getXwl(xwl);
		if(null==json)return ;
		Boolean loginRequired = json.getBoolean("loginRequired");
		if(null==loginRequired || !loginRequired)return ;
		JSONArray array = json.optJSONArray("children");
		if(null==array || array.length()==0) return ;
		JSONObject json_ = ((JSONObject) array.get(0)).optJSONObject("configs");
		Boolean logAccess = false;
		try {
			logAccess = json_.getBoolean("logAccess");
		}catch(Exception e) {}
		if(null==logAccess || !logAccess)return;
		expireCache.put(sessionid+"@"+xwl, true);
		String logMessage = "";
		try {
			logMessage = json_.getString("logMessage");
		}catch(Exception e) {}
		execute(request, logMessage);
	}
	public abstract void execute(HttpServletRequest request, String logMessage);
	
}
